package com.dhcc.core.api.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.core.api.constant.ApiConst;
import com.dhcc.core.framework.base.warpper.BaseControllerWarpper;
import com.dhcc.core.framework.util.HttpContextUtil;
import com.dhcc.core.modules.system.dao.UserMapper;
import com.dhcc.core.modules.system.entity.User;

/**
 * BaseApiController
 * 
 * @ClassName: BaseApiController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:05:08
 */
public class BaseApiController {

    @Autowired
    private UserMapper userMapper;

    protected HttpServletRequest getHttpServletRequest() {
        return HttpContextUtil.getRequest();
    }

    protected HttpServletResponse getHttpServletResponse() {
        return HttpContextUtil.getResponse();
    }

    protected HttpSession getSession() {
        return HttpContextUtil.getRequest().getSession();
    }

    protected HttpSession getSession(Boolean flag) {
        return HttpContextUtil.getRequest().getSession(flag);
    }

    protected String getPara(String name) {
        return HttpContextUtil.getRequest().getParameter(name);
    }

    protected void setAttr(String name, Object value) {
        HttpContextUtil.getRequest().setAttribute(name, value);
    }

    /**
     * 包装一个list，让list增加额外属性
     */
    protected Object warpObject(BaseControllerWarpper warpper) {
        return warpper.warp();
    }

    /**
     * 当前登录用户
     */
    protected User getLoginUser() {
        // 在jwt拦截器里存入userid
        Long userId = Long.parseLong(HttpContextUtil.getRequest().getAttribute(ApiConst.LOGIN_USER_KEY).toString());
        Long deptId = Long.parseLong(HttpContextUtil.getRequest().getAttribute(ApiConst.LOGIN_USER_DEPT_KEY).toString());
        User user = userMapper.selectById(userId);
        user.setDeptId(deptId);
        return user;
    }
}
